package jgjc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
public class FrameMemberServiceController {

    @Resource
    private RestTemplate restTemplate;

    private String serviceUrl = "http://Basic-Data-Management";

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/bridgeSite/getList")
    public JSONArray getList(HttpServletRequest request) {
        int bridgeId = Integer.parseInt(request.getParameter("bridgeId"));
        int sideTypeId = Integer.parseInt(request.getParameter("sideTypeId"));
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/bridgeSite/getList?bridgeId=" + bridgeId + "&sideTypeId=" + sideTypeId, JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getSuperstructureTypeList")
    public JSONArray getSuperstructureTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getSuperstructureTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getMaterialTypeList")
    public JSONArray getMaterialTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getMaterialTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getSupportTypeList")
    public JSONArray getSupportTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getSupportTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getPierTypeList")
    public JSONArray getPierTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getPierTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getAbutmentTypeList")
    public JSONArray getAbutmentTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getAbutmentTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getExpansionJointTypeList")
    public JSONArray getExpansionJointTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getExpansionJointTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListBySiteNo")
    public JSONArray getListBySiteNo(HttpServletRequest request) {
        int siteId = Integer.parseInt(request.getParameter("siteId") == "" ? "0" : request.getParameter("siteId"));
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListBySiteNo?siteId=" + siteId, JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/bridgeSite/isSpanCombinationValid")
    public Map<String, Object> isSpanCombinationValid(HttpServletRequest request) {
        int bridgeId = Integer.parseInt(request.getParameter("bridgeId"));
        int sideTypeId = Integer.parseInt(request.getParameter("sideTypeId"));
        String bridgeSpanCombination = request.getParameter("bridgeSpanCombination");
        JSONObject jsonObject = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/bridgeSite/isSpanCombinationValid?bridgeId=" + bridgeId + "&sideTypeId=" + sideTypeId + "&bridgeSpanCombination=" + bridgeSpanCombination, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/bridgeSite/addSite")
    public JSONObject addSite(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/bridgeSite/addSite", requestMap, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addPier")
    public JSONObject addPier(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/bridgeSite/addSite", requestMap, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addSiteRange")
    public JSONObject addSiteRange(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/addSiteRange", requestMap, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addSuperstructure")
    public JSONObject addSuperstructure(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/addSuperstructure", requestMap, JSONObject.class);
        return jsonObject;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByPier")
    public JSONArray getListByPier() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByPier", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByRiverbed")
    public JSONArray getListByRiverbed() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByRiverbed", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByRegulatingStructure")
    public JSONArray getListByRegulatingStructure() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByRegulatingStructure", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByDeck")
    public JSONArray getListByDeck() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByDeck", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByExpansionJoint")
    public JSONArray getListByExpansionJoint() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByExpansionJoint", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListBySuperstructure")
    public JSONArray getListBySuperstructure(HttpServletRequest request) {
        int superstructureTypeId = Integer.valueOf(request.getParameter("superstructureTypeId"));
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListBySuperstructure?superstructureTypeId=" + superstructureTypeId, JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/getListByAbutment")
    public JSONArray getListByAbutment(HttpServletRequest request) {
        int abutmentTypeId = Integer.valueOf(request.getParameter("abutmentTypeId"));
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/getListByAbutment?abutmentTypeId=" + abutmentTypeId, JSONArray.class);
        return result;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addAbutment")
    public JSONObject addAbutment(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/addAbutment", requestMap, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addDeck")
    public JSONObject addDeck(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/addDeck", requestMap, JSONObject.class);
        return jsonObject;
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/addExpansionJoint")
    public JSONObject addExpansionJoint(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/addExpansionJoint", requestMap, JSONObject.class);
        return jsonObject;
    }


    @DeleteMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> requestMap) {
        ResponseEntity<JSONObject> result = restTemplate.exchange(serviceUrl + "/bridge/bridgeSide/bridgeParts/delete?id=" + requestMap.get("id"), HttpMethod.DELETE, null, JSONObject.class);
        return result.getBody();
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/bridgeParts/edit")
    public JSONObject edit(@RequestBody Map<String, Object> requestMap) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/bridgeParts/edit", requestMap, JSONObject.class);
        return jsonObject;
    }


}
